﻿using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace BaseSyntax.NetTest
{
	public class ConnectServer:ITest
	{
		public void UseWebRequest()
		{
			var url = "http://192.168.5.196:31392/frontend";

			var request = WebRequest.Create(url);
			request.Method = "POST";

			string str = "{\"server_id\":2,\"UId\":\"vzsfjekulyip\",\"DeviceType\":\"iPhone\",\"serverId\":2,\"app_id\":1020,\"appId\":1020,\"PlaySecond\":0,\"ActId\":10000,\"DeviceOs\":\"ios\",\"DeviceId\":\"593569urjjsdlfdw\",\"Ver\":\"3.401\",\"actionid\":10000,\"version\":1.8,\"jwt\":\"\",\"T\":0,\"LoginType\":\"radical\",\"Lang\":\"zh-cn\",\"RetailId\":1}";
        	
			byte[] byteArray = Encoding.UTF8.GetBytes(str);
        	
			request.ContentType = "application/json";
			request.ContentLength = byteArray.Length;
        	
			using (var reqStream = request.GetRequestStream())
			{
				reqStream.Write(byteArray, 0, byteArray.Length);
			}

			var response = request.GetResponse();
        	
			using (var respStream = response.GetResponseStream())
			{
				using (var reader = new StreamReader(respStream))
				{
					string data = reader.ReadToEnd();
					Console.WriteLine(data);
				}
        		
			}
		}
		
		static readonly HttpClient client = new HttpClient();
		
		public static async Task UseHttpClient()
		{
		
			string str = "{\"server_id\":2,\"UId\":\"vzsfjekulyip\",\"DeviceType\":\"iPhone\",\"serverId\":2,\"app_id\":1020,\"appId\":1020,\"PlaySecond\":0,\"ActId\":10000,\"DeviceOs\":\"ios\",\"DeviceId\":\"593569urjjsdlfdw\",\"Ver\":\"3.401\",\"actionid\":10000,\"version\":1.8,\"jwt\":\"\",\"T\":0,\"LoginType\":\"radical\",\"Lang\":\"zh-cn\",\"RetailId\":1}";

			var data = new StringContent(str, Encoding.UTF8, "application/json");

			var url = "http://192.168.5.196:31392/frontend";
			
			
			HttpResponseMessage response = await client.PostAsync(url, data);
			response.EnsureSuccessStatusCode();
			
			string result = await response.Content.ReadAsStringAsync();
			
			Console.WriteLine(result);
		}
		public void Test()
		{
			// UseWebRequest();
			Task task = UseHttpClient();
			task.Wait();
		}
    }
}